﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using WebP.Net;

namespace FanaticRock.Theme.Helpers
{
    public static class PngHelper
    {
        public static void SaveToPng(string filePath, byte[] data)
        {
            //var image = ImageHelper.CreateImageSource(data);
            //var pngEncoder = new PngBitmapEncoder();
            //pngEncoder.Frames.Add(BitmapFrame.Create(image));
            //using var fs = File.OpenWrite(filePath);
            //pngEncoder.Save(fs);
        }

        public static byte[] GetBytes(byte[] data)
        {
            using var ms = new MemoryStream(data);
            using var bitmap = Bitmap.FromStream(ms);

            using var ms2 = new MemoryStream();
            bitmap.Save(ms2, ImageFormat.Png);

            return ms2.ToArray();
            //var image = ImageHelper.CreateImageSource(data);
            //var pngEncoder = new PngBitmapEncoder();
            //pngEncoder.Frames.Add(BitmapFrame.Create(image));
            //using var ms = new MemoryStream();
            //pngEncoder.Save(ms);

            //return ms.ToArray();
        }

        public static byte[] GetBytesFromWebP(byte[] data)
        {
            using var webp = new WebPObject(data);
            var bitmap =  webp.GetImage();

            using var ms2 = new MemoryStream();
            bitmap.Save(ms2, ImageFormat.Png);

            return ms2.ToArray();
        } 
    }

}
